import com.sun.org.apache.regexp.internal.REUtil;

import javax.swing.text.html.HTMLWriter;
import java.util.LinkedList;

public class SingleLinkedList {
    static class ListNode {
        public int value;
        public ListNode next;
        public ListNode(int value) {
            this.value = value;
        }
    }
    private ListNode head;
    private int size;

    public void createList () {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        size = 5;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
        }else {
            node.next = head;
            head = node;
        }
        size++;
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
        }else {
            ListNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next=node;
        }
        size++;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        ListNode node = new ListNode(data);
        if(index <0 || index>size) {  //判断插入位置合法性
            System.out.println("插入位置不合法");
            return false;
        }
        if(index == 0) {
            addFirst(data);
            return true;
        }
        ListNode cur = head;
        for(int i=0; i<index-1; ++i) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next  = node;
        size++;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public boolean remove(int key) {
        ListNode cur = head;
        if(cur.value == key) {
            head = head.next;
            size--;
            return true;
        }
        while (cur.next !=null ) {  //找到key的前一个节点
            if(cur.next.value == key) {
                cur.next = cur.next.next;
                size--;
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        while (remove(key)) {}
    }
    //得到单链表的长度
    public int size() {
        return size;
    }
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        head = null;
        size = 0;
    }
}
